package com.example.crmdemo.Crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.crmdemo.Crm.entity.*;
import com.example.crmdemo.Crm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/workorder")
public class WorkorderController {

    @Autowired
    private WorkorderService workorderService;

    @Autowired
    private OperationService operationService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private PersonService personService;

    // 添加工单
    @PostMapping("/add")
    public boolean addWorkorder(@RequestBody Workorder workorder) {
        return workorderService.save(workorder);
    }

    // 删除工单
    @DeleteMapping("/delete/{id}")
    public boolean deleteWorkorder(@PathVariable Long id) {
        return workorderService.removeById(id);
    }

    // 修改工单信息
    @PutMapping("/update")
    public boolean updateWorkorder(@RequestBody Workorder workorder) {
        return workorderService.updateById(workorder);
    }

    // 按业务名或客户名查询工单
    @GetMapping("/get")
    public List<Workorder> getWorkorderByOperationOrCustomer(
            @RequestParam(required = false) String operationName,
            @RequestParam(required = false) String customerName) {
        QueryWrapper<Workorder> queryWrapper = new QueryWrapper<>();
        if (operationName != null && !operationName.isEmpty()) {
            queryWrapper.or().eq("o_name", operationName);
        }
        if (customerName != null && !customerName.isEmpty()) {
            queryWrapper.or().eq("c_name", customerName);
        }
        return workorderService.list(queryWrapper);
    }

    // 获取所有工单信息
    @GetMapping("/getall")
    public List<Workorder> getAllWorkorders() {
        return workorderService.list();
    }

    // 获取所有业务信息
    @GetMapping("/operations")
    public List<Operation> getAllOperations() {
        return operationService.list();
    }

    // 获取所有客户信息
    @GetMapping("/customers")
    public List<Customer> getAllCustomers() {
        return customerService.list();
    }

    // 获取所有区域信息
    @GetMapping("/areas")
    public List<Area> getAllAreas() {
        return areaService.list();
    }

    // 获取所有人员信息
    @GetMapping("/persons")
    public List<Person> getAllPersons() {
        return personService.list();
    }

    // 派单
    @PutMapping("/assign")
    public boolean assignWorkorder(@RequestParam Long id, @RequestParam String person) {
        Workorder workorder = workorderService.getById(id);
        if (workorder != null) {
            workorder.setPerson(person);
            workorder.setStatus("已派单");
            return workorderService.updateById(workorder);
        }
        return false;
    }

    // 收单
    @PutMapping("/receive")
    public boolean receiveWorkorder(@RequestParam Long id) {
        Workorder workorder = workorderService.getById(id);
        if (workorder != null) {
            workorder.setStatus("已收单");
            return workorderService.updateById(workorder);
        }
        return false;
    }
}
